package com.zhuss.core.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class WebConfig implements WebMvcConfigurer {
	
	@Value("${server.ip}")
	private String ip;
	
	@Value("${server.servlet.context-path}")
	private String contextPath;
	
	@Autowired
	private FeignAuthenticationInterceptor feignAuthenticationInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(feignAuthenticationInterceptor).addPathPatterns("/**").excludePathPatterns("/error"
        		);
    }
    
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/aabFile/**", 
				"/static/**", "classpath:/static/", "swagger-ui.html", "classpath:/META-INF/resources/", "/webjars/**", "classpath:/META-INF/resources/webjars/"
				)
		.addResourceLocations("file:/home/file/aabFile/upload/").addResourceLocations("file:E:/project/aab/upload/").addResourceLocations("file:D:/project/aab/upload/")
		.addResourceLocations("/static/**").addResourceLocations("classpath:/static/").addResourceLocations("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/").addResourceLocations("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		WebMvcConfigurer.super.addResourceHandlers(registry);
	}
	
	
	/**
     * 通过 createRestApi函数来构建一个DocketBean
     * 函数名,可以随意命名,喜欢什么命名就什么命名
     * 接口文档默认访问路径http://localhost:8080/swagger-ui.html，
     *          配置文件中有配置此处为http://localhost:8080/springboot2/swagger-ui.html
     * 注解说明参考博客：https://blog.csdn.net/qq_28009065/article/details/79104103，
     */
    @Bean
    public Docket commonDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
        		.host(ip)
                .groupName(contextPath.substring(1) + "-API接口文档")
                .apiInfo(apiInfo(contextPath.substring(1) + "-测试环境接口"))
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhuss.controller"))//指向自己的controller即可
                .paths(PathSelectors.any())
                .build();
    }
    
    //	设置文档信息
    private ApiInfo apiInfo(String desc) {
        return new ApiInfoBuilder()
                //页面标题
                .title(desc)
                //设置作者联系方式,可有可无
                .contact(new Contact("zhushanshuo", "https://com.zhuss.cn", "zhushanshuo@zhuss.com"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
 
    }
}
